package com.piece.spider.controller;

import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.enums.sql.SqlKey;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.Order;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.module.spider.model.Function;
import com.piece.module.spider.service.FunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;

@RestController
@Api(tags = {"爬虫函数管理"})
@RequestMapping(value = "/api/spider/function")
public class SpiderFunctionController extends FrameWorkController {

    @Autowired
    private FunctionService functionService;

    @ApiOperation(value = "查询爬虫函数列表")
    @RequestMapping("/table")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse modelTable() {
        QueryTable queryTable = initTable();
        queryTable.setOrder(Collections.singletonList(new Order("createDate", SqlKey.DESC)));
        DataTable<Function> dataTable = functionService.findPage(queryTable);
        return success(dataTable);
    }

    @RequestMapping("/load/{id}")
    @ApiOperation(value = "获取爬虫函数")
    @ApiImplicitParam(name="id", value = "爬虫函数ID", type = "path")
    public AjaxResponse load(@PathVariable Long id) {
        return success(functionService.findById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存爬虫函数")
    @ApiImplicitParam(value = "爬虫函数", dataTypeClass = Function.class, type = "body")
    public AjaxResponse save() {
        Function function = new Function();
        try {
            initDB(function);
            functionService.insert(function);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS));
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), ExceptionUtils.getStackTrace(e));
        }
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除爬虫函数")
    @ApiImplicitParam(name = "id", value = "爬虫函数ID", type = "path")
    public AjaxResponse delete(@PathVariable Long id) {
        try {
            functionService.delete(id);
            return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS));
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.DELETE_FAILURE), ExceptionUtils.getStackTrace(e));
        }
    }
}
